home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Washington_1988 / DevCon88.1 / AmigaTechniques / font2asm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  12.7 KB  |  430 lines

  1. /****** Font2Asm *****************************************************
  2. *
  3. *   NAME
  4. *       Font2Asm - create an assembly file from a font image
  5. *
  6. *   A hack, nothing more
  7. *
  8. *  Copyright (c) 1988 Commodore-Amiga, Inc.
  9. *  Executables based on this information may be used in software
  10. *  for Commodore Amiga computers.  All other rights reserved.
  11. *  This information is provided "as is"; no warranties are made.
  12. *  All use is at your own risk, and no liability or responsibility is assumed.
  13. *
  14. *********************************************************************/
  15. #include        "exec/types.h"
  16. #include        "exec/nodes.h"
  17. #include        "exec/lists.h"
  18. #include        "exec/memory.h"
  19. #include        "exec/ports.h"
  20. #include    "graphics/gfxbase.h"
  21. #include    "graphics/text.h"
  22. #include    "graphics/view.h"
  23. #include        "libraries/dos.h"
  24. #include    "libraries/diskfont.h"
  25.  
  26. #include    "stdio.h"
  27.  
  28. #ifdef    MANX
  29. #include    "functions.h"
  30. #endif
  31.  
  32. #ifndef    ColorTextFont
  33. #define    FSB_COLORFONT    6    /* this uses ColorTextFont structure */
  34. #define    FSF_COLORFONT    0x40
  35.  
  36. /******    ColorTextFont node ******************************************/
  37. /*-----    ctf_Flags --------------------------------------------------*/
  38. #define    CT_COLORMASK    0x000F    /* mask to get to following color styles */
  39. #define    CT_COLORFONT    0x0001    /* color map contains designer's colors */
  40. #define    CT_GREYFONT    0x0002    /* color map describes even-stepped */
  41.                 /* brightnesses from low to high */
  42. #define    CTB_MAPCOLOR    0    /* map ctf_FgColor to the rp_FgPen if it's */
  43. #define    CTF_MAPCOLOR    0x0001    /* is a valid color within ctf_Low..ctf_High */
  44.  
  45. /*-----    ColorTextFont ----------------------------------------------*/
  46. struct ColorTextFont {
  47.     struct TextFont ctf_TF;
  48.     UWORD    ctf_Flags;    /* extended flags */
  49.     UBYTE    ctf_Depth;    /* number of bit planes */
  50.     UBYTE    ctf_FgColor;    /* color that is remapped to FgPen */
  51.     UBYTE    ctf_Low;    /* lowest color represented here */
  52.     UBYTE    ctf_High;    /* highest color represented here */
  53.     UBYTE    ctf_PlanePick;    /* PlanePick ala Images */
  54.     UBYTE    ctf_PlaneOnOff;    /* PlaneOnOff ala Images */
  55.     APTR    ctf_ColorMap;    /* struct ColorMap * for font */
  56.     APTR    ctf_CharData[8]; /*pointers to bit planes ala tf_CharData */
  57. };
  58. #endif
  59.  
  60. struct ColorTextFont *FindName();
  61. struct GfxBase *OpenLibrary();
  62.  
  63. FILE *file = 0;
  64.  
  65. int GetNum(b)
  66. char *b;
  67. {
  68.     int result, base;
  69.  
  70.     result = 0;
  71.     base = 10;
  72.     while (*b) {
  73.     result *= base;
  74.     if ((*b >= '0') && (*b <= '9')) result += *b - '0';
  75.     else {
  76.         if (((*b >= 'a') && (*b <= 'f')) ||
  77.             ((*b >= 'A') && (*b <= 'F'))) {
  78.         if (base != 16) break;
  79.         result += (*b & 0x1f) + 9;
  80.         }
  81.         else if ((*b != 'x') && (*b != '$')) base = 16;
  82.         else break;
  83.     }
  84.     b++;
  85.     }
  86.     if (*b) {
  87.     printf("ERROR: ill formed number\n");
  88.     return(-1);
  89.     }
  90.     else return(result);
  91. }
  92.  
  93.  
  94. dataDump(data, height, width)
  95. UWORD *data, height, width;
  96. {
  97.     UWORD col;
  98.  
  99.     for (; height > 0; height--) {
  100.     col = 0;
  101.     do {
  102.         if ((col & 7) == 0)
  103.         fprintf(file, "\n\t    dc.w    ");
  104.         else fprintf(file, ",");
  105.         fprintf(file, "$%04lx", *data++);
  106.         col++;
  107.     }
  108.         while (col < width);
  109.     fprintf(file, "\n");
  110.     }
  111. }
  112.  
  113.  
  114. main(argc, argv)
  115. int argc;
  116. char *argv[];
  117. {
  118.     struct GfxBase *GfxBase;
  119.     struct ColorTextFont *ctf, *ctf2;
  120.     int i, j, k;
  121.     ULONG *longptr;
  122.     WORD *wordptr;
  123.  
  124.     GfxBase = (struct GfxBase *) OpenLibrary("graphics.library", 0L);
  125.     if (GfxBase == 0) {
  126.     printf("ERROR: Cannot open \"graphics.library\"\n");
  127.     exit(100);
  128.     }
  129.     if (argc >= 3) {
  130.     file = fopen(argv[1], "w");
  131.     if (file == 0) {
  132.         printf("ERROR: open failed for font file\n");
  133.         argc = 0;
  134.     }
  135.     }
  136.     if (argc >= 3) {
  137.       /* see if more than one with this name */
  138.       ctf = FindName(&GfxBase->TextFonts, argv[2]);
  139.       if (ctf) {
  140.         if (FindName(ctf, argv[2])) {
  141.           /* yes: get the one with this size */
  142.           if (argc >= 4) {
  143.             i = GetNum(argv[3]);
  144.             if (i >= 0) {
  145.               while ((ctf) && (ctf->ctf_TF.tf_YSize != i))
  146.                 ctf = FindName(ctf, argv[2]);
  147.               if (ctf) {
  148.                 /* check for more than one */
  149.                 ctf2 = FindName(ctf, argv[2]);
  150.                 while ((ctf2) && (ctf2->ctf_TF.tf_YSize != i))
  151.                   ctf2 = FindName(ctf2, argv[2]);
  152.                 if (ctf2) {
  153.                   /* yes: get the one with this style */
  154.           if (argc >= 5) {
  155.             j = GetNum(argv[4]);
  156.             if (j >= 0) {
  157.               ctf = FindName(&GfxBase->TextFonts, argv[2]);
  158.               while ((ctf) && ((ctf->ctf_TF.tf_YSize != i) ||
  159.               (ctf->ctf_TF.tf_Style != j)))
  160.             ctf = FindName(ctf, argv[2]);
  161.               if (ctf) {
  162.             /* check for more than one */
  163.             ctf2 = FindName(ctf, argv[2]);
  164.             while ((ctf2) && ((ctf2->ctf_TF.tf_YSize != i) ||
  165.                 (ctf2->ctf_TF.tf_Style != j)))
  166.               ctf2 = FindName(ctf2, argv[2]);
  167.             if (ctf2) {
  168.               /* yes: get the one with this flags */
  169.               if (argc >= 6) {
  170.                 k = GetNum(argv[5]);
  171.                 if (k >= 0) {
  172.                   ctf = FindName(&GfxBase->TextFonts, argv[2]);
  173.                   while ((ctf) && ((ctf->ctf_TF.tf_YSize != i) ||
  174.                   (ctf->ctf_TF.tf_Style != j) ||
  175.                   (ctf->ctf_TF.tf_Flags != k)))
  176.                 ctf = FindName(ctf, argv[2]);
  177.                   if (ctf) {
  178.                 /* check for more than one */
  179.                 ctf2 = FindName(ctf, argv[2]);
  180.                 while ((ctf2) && ((ctf2->ctf_TF.tf_YSize != i)
  181.                     || (ctf->ctf_TF.tf_Style != j) ||
  182.                     (ctf->ctf_TF.tf_Flags != k)))
  183.                   ctf2 = FindName(ctf2, argv[2]);
  184.                 if (ctf2) {
  185.                   printf("WARNING: There exists more than one font matching the specification.\n");
  186.                   printf("         The first one found is being used.\n");
  187.                                 }
  188.                   }
  189.                   else /* ctf */ {
  190.                 printf("ERROR: No font with the specified flags\n");
  191.                 argc = 0;
  192.                   }
  193.                 }
  194.                 else /* k */ argc = 0;
  195.               }
  196.               else /* argc */ {
  197.                 printf("ERROR: More than one font with the specified name, size, and style\n");
  198.                 argc = 0;
  199.               }
  200.             }
  201.               }
  202.               else /* ctf */ {
  203.                 printf("ERROR: No font with the specified flags\n");
  204.             argc = 0;
  205.               }
  206.             }
  207.             else /* j */ argc = 0;
  208.           }
  209.           else /* argc */ {
  210.             printf("ERROR: More than one font with the specified name, and size\n");
  211.             argc = 0;
  212.           }
  213.         }
  214.           }
  215.           else /* ctf */ {
  216.         printf("ERROR: No font with the specified size\n");
  217.         argc = 0;
  218.           }
  219.             }
  220.             else /* i */ argc = 0;
  221.           }
  222.           else /* argc */ {
  223.             printf("ERROR: More than one font with the specified name\n");
  224.             argc = 0;
  225.           }
  226.         }
  227.       }
  228.       else /* ctf */ {
  229.         printf("ERROR: No font with the specified name\n");
  230.         argc = 0;
  231.       }
  232.       CloseLibrary(GfxBase);
  233.     }
  234.     if (argc < 3) {
  235.     printf(
  236.       "USAGE: Font2Asm <file> <font-name> [<size>] [<style>] [<flags>]\n");
  237.     if (file) fclose(file);
  238.     exit(5);
  239.     }
  240.     ctf->ctf_TF.tf_Accessors++;
  241.     printf("\"%s\" Y %ld S $%02lx F $%02lx\n",
  242.         ctf->ctf_TF.tf_Message.mn_Node.ln_Name, ctf->ctf_TF.tf_YSize,
  243.         ctf->ctf_TF.tf_Style, ctf->ctf_TF.tf_Flags);
  244.  
  245.     /* write out the header */
  246.     fprintf(file, ";------ Included Files ------\n");
  247.     fprintf(file, "\n");
  248.     fprintf(file, "    INCLUDE        \"exec/types.i\"\n");
  249.     fprintf(file, "    INCLUDE        \"exec/nodes.i\"\n");
  250.     fprintf(file, "    INCLUDE        \"libraries/diskfont.i\"\n");
  251.     fprintf(file, "\n");
  252.     fprintf(file, "        moveq    #100,D0\n");
  253.     fprintf(file, "        rts\n");
  254.     fprintf(file, "\n");
  255.     fprintf(file, "        dc.l    0        ; ln_Succ\n");
  256.     fprintf(file, "        dc.l    0        ; ln_Pred\n");
  257.     fprintf(file, "        dc.b    NT_FONT        ; ln_Type\n");
  258.     fprintf(file, "        dc.b    0        ; ln_Pri\n");
  259.     fprintf(file, "        dc.l    fontName    ; ln_Name\n");
  260.     fprintf(file, "        dc.w    DFH_ID        ; FileID\n");
  261.     fprintf(file, "        dc.w    0        ; Revision\n");
  262.     fprintf(file, "        dc.l    0        ; Segment\n");
  263.     fprintf(file, "fontName:\n");
  264.     fprintf(file, "        dcb.b    MAXFONTNAME,0    ; Name\n");
  265.     fprintf(file, "\n");
  266.     fprintf(file, "font:\n");
  267.     fprintf(file, "        dc.l    0        ; ln_Succ\n");
  268.     fprintf(file, "        dc.l    0        ; ln_Pred\n");
  269.     fprintf(file, "        dc.b    NT_FONT        ; ln_Type\n");
  270.     fprintf(file, "        dc.b    0        ; ln_Pri\n");
  271.     fprintf(file, "        dc.l    fontName    ; ln_Name\n");
  272.     fprintf(file, "        dc.l    0        ; mn_ReplyPort\n");
  273.     fprintf(file, "        dc.w    fontEnd-font    ; mn_Length\n");
  274.     fprintf(file, "        dc.w    %ld        ; tf_YSize\n",
  275.         ctf->ctf_TF.tf_YSize);
  276.     fprintf(file, "        dc.b    $%02lx        ; tf_Style\n",
  277.         ctf->ctf_TF.tf_Style);
  278.     fprintf(file, "        dc.b    $%02lx        ; tf_Flags\n",
  279.         ctf->ctf_TF.tf_Flags);
  280.     fprintf(file, "        dc.w    %ld        ; tf_XSize\n",
  281.         ctf->ctf_TF.tf_XSize);
  282.     fprintf(file, "        dc.w    %ld        ; tf_Baseline\n",
  283.         ctf->ctf_TF.tf_Baseline);
  284.     fprintf(file, "        dc.w    %ld        ; tf_BoldSmear\n",
  285.         ctf->ctf_TF.tf_BoldSmear);
  286.     fprintf(file, "        dc.w    0        ; tf_Accessors\n");
  287.     fprintf(file, "        dc.b    %ld        ; tf_LoChar\n",
  288.         ctf->ctf_TF.tf_LoChar);
  289.     fprintf(file, "        dc.b    %ld        ; tf_HiChar\n",
  290.         ctf->ctf_TF.tf_HiChar);
  291.     fprintf(file, "        dc.l    fontData    ; tf_CharData\n");
  292.     fprintf(file, "        dc.w    %ld        ; tf_Modulo\n",
  293.         ctf->ctf_TF.tf_Modulo);
  294.     fprintf(file, "        dc.l    fontLoc        ; tf_CharLoc\n");
  295.     if (ctf->ctf_TF.tf_CharSpace)
  296.     fprintf(file, "        dc.l    fontSpace    ; tf_CharSpace\n");
  297.     else
  298.     fprintf(file, "        dc.l    0        ; tf_CharSpace\n");
  299.     if (ctf->ctf_TF.tf_CharKern)
  300.     fprintf(file, "        dc.l    fontKern    ; tf_CharKern\n");
  301.     else
  302.     fprintf(file, "        dc.l    0        ; tf_CharKern\n");
  303.     fprintf(file, "\n");
  304.     if (ctf->ctf_TF.tf_Style & FSF_COLORFONT) {
  305.     fprintf(file, "        dc.w    $%04lx        ; ctf_Flags\n",
  306.         ctf->ctf_Flags);
  307.     fprintf(file, "        dc.b    %ld        ; ctf_Depth\n",
  308.         ctf->ctf_Depth);
  309.     fprintf(file, "        dc.b    %ld        ; ctf_FgColor\n",
  310.         ctf->ctf_FgColor);
  311.     fprintf(file, "        dc.b    %ld        ; ctf_Low\n",
  312.         ctf->ctf_Low);
  313.     fprintf(file, "        dc.b    %ld        ; ctf_High\n",
  314.         ctf->ctf_High);
  315.     fprintf(file, "        dc.b    $%02lx        ; ctf_PlanePick\n",
  316.         ctf->ctf_PlanePick);
  317.     fprintf(file, "        dc.b    $%02lx        ; ctf_PlaneOnOff\n",
  318.         ctf->ctf_PlaneOnOff);
  319.     if (ctf->ctf_ColorMap &&
  320.         (((struct ColorMap *) ctf->ctf_ColorMap)->Type == 0))
  321.         fprintf(file, "\t    dc.l    colorMap    ; ctf_ColorMap\n");
  322.     else
  323.         fprintf(file, "\t    dc.l    0        ; ctf_ColorMap\n");
  324.     for (i = 0; i < ctf->ctf_Depth; i++)
  325.         fprintf(file, "\t    dc.l    colorData%ld    ; ctf_CharData[%ld]\n",
  326.             i, i);
  327.     for (; i < 8; i++)
  328.         fprintf(file, "\t    dc.l    0        ; ctf_CharData[%ld]\n",
  329.             i);
  330.     if (ctf->ctf_ColorMap &&
  331.         (((struct ColorMap *) ctf->ctf_ColorMap)->Type == 0))
  332.         fprintf(file, "\n");
  333.         fprintf(file, "colorMap:\n");
  334.         fprintf(file, "\t    dc.b    $%02lx        ; Flags\n",
  335.             ((struct ColorMap *) ctf->ctf_ColorMap)->Flags);
  336.         fprintf(file, "\t    dc.b    0        ; Type\n");
  337.         fprintf(file, "\t    dc.b    %ld        ; Count\n",
  338.             ((struct ColorMap *) ctf->ctf_ColorMap)->Count);
  339.         fprintf(file, "\t    dc.l    colorTable    ; ColorTable\n");
  340.         fprintf(file, "\n");
  341.         fprintf(file, "colorTable:\n");
  342.         for (i = 0; i < ((struct ColorMap *) ctf->ctf_ColorMap)->Count; i++)
  343.         fprintf(file, "\t\tdc.w    $%04lx        ; (color %ld)\n",
  344.             ((UWORD *) ((struct ColorMap *) ctf->ctf_ColorMap)
  345.             ->ColorTable)[i], i);
  346.     }
  347.     fprintf(file, "\n");
  348.     fprintf(file, "fontLoc:");
  349.     i = ctf->ctf_TF.tf_LoChar-1;
  350.     longptr = (ULONG *) ctf->ctf_TF.tf_CharLoc;
  351.     j = 0;
  352.     do {
  353.     if (j == 0)
  354.         fprintf(file, "\n\t    dc.l    ");
  355.     else fprintf(file, ",");
  356.     fprintf(file, "$%08lx", *longptr++);
  357.     j++;
  358.     j &= 3;
  359.     i++;
  360.     }
  361.     while (i <= ctf->ctf_TF.tf_HiChar);
  362.     fprintf(file, "\n");
  363.  
  364.     if (ctf->ctf_TF.tf_CharSpace) {
  365.     fprintf(file, "fontSpace:");
  366.     i = ctf->ctf_TF.tf_LoChar-1;
  367.     wordptr = (WORD *) ctf->ctf_TF.tf_CharSpace;
  368.     j = 0;
  369.     do {
  370.         if (j == 0)
  371.         fprintf(file, "\n\t    dc.w    ");
  372.         else fprintf(file, ",");
  373.         fprintf(file, "%04ld", *wordptr++);
  374.         j++;
  375.         j &= 7;
  376.         i++;
  377.     }
  378.         while (i <= ctf->ctf_TF.tf_HiChar);
  379.     fprintf(file, "\n");
  380.     }
  381.  
  382.     if (ctf->ctf_TF.tf_CharKern) {
  383.     fprintf(file, "fontKern:");
  384.     i = ctf->ctf_TF.tf_LoChar-1;
  385.     wordptr = (WORD *) ctf->ctf_TF.tf_CharKern;
  386.     j = 0;
  387.     do {
  388.         if (j == 0)
  389.         fprintf(file, "\n\t    dc.w    ");
  390.         else fprintf(file, ",");
  391.         fprintf(file, "%04ld", *wordptr++);
  392.         j++;
  393.         j &= 7;
  394.         i++;
  395.     }
  396.         while (i <= ctf->ctf_TF.tf_HiChar);
  397.     fprintf(file, "\n");
  398.     }
  399.  
  400.     if (ctf->ctf_TF.tf_Style & FSF_COLORFONT) {
  401.     for (i = 0; i < ctf->ctf_Depth; i++) {
  402.         for (j = 0; j < i; j++)
  403.         if (ctf->ctf_CharData[i] == ctf->ctf_CharData[j]) break;
  404.         if (i == j) {
  405.         if (ctf->ctf_TF.tf_CharData == ctf->ctf_CharData[i])
  406.             fprintf(file, "fontData:\n");
  407.         fprintf(file, "colorData%ld:", i);
  408.         for (j = i; j < ctf->ctf_Depth; j++)
  409.             if (ctf->ctf_CharData[i] == ctf->ctf_CharData[j])
  410.             fprintf(file, "\ncolorData%ld:", j);
  411.         dataDump(ctf->ctf_CharData[i],
  412.             ctf->ctf_TF.tf_YSize, ctf->ctf_TF.tf_Modulo/2);
  413.         }
  414.     }
  415.     }
  416.     else {
  417.     fprintf(file, "fontData:");
  418.     dataDump(ctf->ctf_TF.tf_CharData,
  419.         ctf->ctf_TF.tf_YSize, ctf->ctf_TF.tf_Modulo/2);
  420.     }
  421.     fprintf(file, "fontEnd:\n");
  422.     fprintf(file, "\n");
  423.     fprintf(file, "    END\n");
  424.  
  425.     ctf->ctf_TF.tf_Accessors--;
  426.     fclose(file);
  427. }
  428.